package rmi.client;

import gui.OptionPane;

import java.util.ArrayList;
import java.util.HashMap;

import model.CardInterface;
import model.ChosenAction;
import model.UserInterface;
import data.SeatInterface;

public class ClientRemote implements Client
{
	private UserInterface user;
	
	private HashMap<Integer, controller.Table> tables = new HashMap<Integer, controller.Table>();
	
	public ClientRemote()
	{
		
	}
	
	public void setUser(UserInterface user)
	{
		this.user = user;
	}
	
	public UserInterface getUser()
	{
		return user;
	}
	
	public void watchGame(int gameId, controller.Table table)
	{
		tables.put(gameId, table);
	}

	public void leaveGame(int gameId)
	{
		tables.remove(gameId);
	}
	
	public void updateBoard(int gameId, ArrayList<CardInterface> cards)
	{
		controller.Table table = tables.get(gameId);
		
		if (table != null)
		{
			table.getView().updateBoard(cards);
		}
	}

	public void updateHand(int gameId, ArrayList<CardInterface> cards)
	{
		controller.Table table = tables.get(gameId);
		
		if (table != null)
		{
			table.getView().updateHand(cards);
		}
	}
	
	public ChosenAction chooseAction(int gameId)
	{
		controller.Table table = tables.get(gameId);
		
		if (table != null)
		{
			return table.getView().chooseAction();
		}
		
		return null;
	}
	
	public void setTurnSeatIndex(int gameId, int seatIndex)
	{
		controller.Table table = tables.get(gameId);
		
		if (table != null)
		{
			table.getView().setTurnSeatIndex(seatIndex);
		}
	}
	
	public void updateSeat(SeatInterface seat)
	{
		controller.Table table = tables.get(seat.getGameId());
		
		if (table != null)
		{
			table.getView().updateSeat(seat);
		}
	}
	
	public void sendChatMessage(int gameId, UserInterface user, String message)
	{
		controller.Table table = tables.get(gameId);
		
		if (table != null)
		{
			table.getView().sendChatMessage(user, message);
		}
	}
	
	public void serverClosed()
	{
		OptionPane.showMessage("Server closed", "The server closed. So the client will be closed too.");
		
		System.exit(0);
	}
}
